package com.erpanderp.wam.tm.model.service;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.FlushMode;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;

import com.erpanderp.wam.adm.model.entity.SAPERPModule;
import com.erpanderp.wam.tm.model.entity.Band;
import com.erpanderp.wam.tm.model.entity.RecruitmentRequest;
import com.erpanderp.wam.tm.model.entity.Skill;
import com.erpanderp.wam.tm.model.entity.Technology;
import com.erpanderp.wam.usm.model.entity.user.Employee;
import com.erpanderp.wam.util.CommonLookup;
import com.erpanderp.wam.util.HibernateUtil;

public class RRFDAO {

	public List<Band> getAllBands() { //BAnd
		List<Band> allBands = null;
		try {
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.COMMIT);
			Query query = session.createQuery("from Band b");
			query.setCacheable(false);
			allBands = (List<Band>) query.list();
			session.getTransaction().commit();

		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return allBands;
	}
	
	public List<Technology> getAllTechnologies() { //Technology
		List<Technology> allTechnologies = null;
		try {
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.COMMIT);
			Query query = session.createQuery("from Technology t");
			query.setCacheable(false);
			allTechnologies = (List<Technology>) query.list();
			session.getTransaction().commit();

		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return allTechnologies;
	}
	
	public List<Employee> getAllRecruiters() {//EmpDAo
		List<Employee> allRecruiters = null;
		String modulNameRecruitment = CommonLookup.MODULENAME_RECRUITMENT;
		System.out.println("MODLE NAME RECRUITER::::::RRF DAO:::::::::"+modulNameRecruitment);
		try {
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.COMMIT);
			Query query = session.createQuery("from Employee e where moduleName=:modulNameRecruitment").setString("modulNameRecruitment", modulNameRecruitment);
			query.setCacheable(false);
			allRecruiters = (List<Employee>) query.list();
			System.out.println("allRecruiters========"+allRecruiters.size());
			session.getTransaction().commit();

		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return allRecruiters;
	}
	
	public List<Employee> getInterviewersByModule(String moduleName){//EMPDAO
		List<Employee> interviewers = new ArrayList<Employee>();
		System.out.println("========moduleName====DAO===="+moduleName);
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.COMMIT);
			String queryString = "from Employee e where e.moduleName = :moduleName";
			Query query = session.createQuery(queryString);
			query.setCacheable(false).setString("moduleName", moduleName);			
			interviewers =(List<Employee>) query.list();
			session.getTransaction().commit();
			System.out.println("interviewers LIST SIZE =========DAO==="+interviewers.size());
			return interviewers;	
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}		
	}
	
 public String getLastGeneratedRRFCode(){
		
		String queryString="select r.recruitmentRequestCode from RecruitmentRequest r ";
		
		System.out.println("QUERY: " + queryString);
		
		List<String> codeList = null;
		
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.getTransaction().begin();		
			Query query = session.createQuery(queryString);
			query.setCacheable(false);
			codeList = (List<String>) query.list();
			session.getTransaction().commit();
		}catch(HibernateException e){
			e.printStackTrace();
		}
		
		String lastCode = null;
		for (String code : codeList) {
			
			if (lastCode == null) {
				lastCode = code;
				continue;
				
			}else if (Long.parseLong(code.substring(11)) > Long.parseLong(lastCode.substring(11))) {
					lastCode = code;
					continue;
				
			}
			System.out.println(lastCode + "============ " + code);
		}		
		return lastCode;	
	}
 
 public boolean isRRFCodeExistOrNot(String rrfCode) {
		boolean isRRFCodeExist = false;
		try {
			Session session = HibernateUtil.getSessionFactory()
					.getCurrentSession();
			session.getTransaction().begin();
			String queryString = "from RecruitmentRequest r where r.recruitmentRequestCode=:rrfCode";
			Query query = session.createQuery(queryString).setString(
					"rrfCode", rrfCode);
			query.setCacheable(false);

			List tempList = query.list();

			if (tempList == null || tempList.size() == 0) {
				isRRFCodeExist = true;
			}

			session.close();
			return isRRFCodeExist;
		} catch (Exception e) {
			e.printStackTrace();
			return isRRFCodeExist;
		}
 }
 
 public RecruitmentRequest createRRF(RecruitmentRequest rrfRequest){
		try{
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.getTransaction().begin();
		System.out.println("FLUSH MODE: " + session.getFlushMode());
		session.setFlushMode(FlushMode.COMMIT);
		session.saveOrUpdate(rrfRequest);
		session.getTransaction().commit();
		return rrfRequest;
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}		
	}
 /*public String updateRRFStatusByRRFCode(String rrfCode){
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.getTransaction().begin();			
			session.setFlushMode(FlushMode.COMMIT);
			session.saveOrUpdate(rrfCode);
			session.getTransaction().commit();
			return rrfCode;
			}catch(Exception e){
				e.printStackTrace();
				return null;
			}		
		}*/
 public SAPERPModule getTechnologyByTechnologyName(String technologyName) {//TECDAO
	 SAPERPModule technology = null;
		try {
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.COMMIT);
			Query query = session.createQuery("from SAPERPModule t where t.displayName=:technologyName").setString(
					"technologyName", technologyName);;
			query.setCacheable(false);
			List<SAPERPModule> tempList = query.list();
			session.getTransaction().commit();
			
			if(tempList != null && tempList.size()>0){
				technology = tempList.get(0);
				return technology;
			}else{
				return null;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}		
	}
 
 public Band getBandByBandId(Long bandId) {//BAND
		Band band = null;
			try {
				Session session = HibernateUtil.getSessionFactory().getCurrentSession();
				session.beginTransaction();
				session.setFlushMode(FlushMode.COMMIT);
				Query query = session.createQuery("from Band b where b.bandId=:bandId").setLong(
						"bandId", bandId);
				query.setCacheable(false);
				List<Band> tempList = query.list();
				session.getTransaction().commit();
				
				if(tempList != null && tempList.size()>0){
					band = tempList.get(0);
					return band;
				}else{
					return null;
				}
			} catch (Exception e) {
				e.printStackTrace();
				return null;
			}		
		}
 public Employee getReportingManager(Long employeeId){ //Employee
		Employee employee ;		
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.COMMIT);
			String queryString = "select er.projectManager from EmployeeAndReporting er where er.employee.employeeId = :employeeId";
			
			Query query = session.createQuery(queryString);
			query.setCacheable(false).setLong("employeeId", employeeId);			
			employee = (Employee) query.uniqueResult();
			session.getTransaction().commit();
			
			return employee;
			
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}	
	}
 public Employee getRecruiterByRecruiterId(Long recruiterId){ //Employee
		Employee employee ;		
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.COMMIT);
			String queryString = "from Employee e where e.employeeId = :recruiterId";
			
			Query query = session.createQuery(queryString);
			query.setCacheable(false).setLong("recruiterId", recruiterId);			
			employee = (Employee) query.uniqueResult();
			session.getTransaction().commit();
			
			return employee;
			
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}	
	}
 public Employee getInterviewerByInterviewerId(Long interviewerId){ //EmployeeDAO
		Employee employee ;		
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.COMMIT);
			String queryString = "from Employee e where e.employeeId = :interviewerId";
			
			Query query = session.createQuery(queryString);
			query.setCacheable(false).setLong("interviewerId", interviewerId);			
			employee = (Employee) query.uniqueResult();
			session.getTransaction().commit();
			
			return employee;
			
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}	
	}
 public List<String> getSkillsByTechnology(Long technologyId){ //SkillDAO
		List<String> primarySkills = new ArrayList<String>();
		List<Skill> pSkills = new ArrayList<Skill>();
		System.out.println("========technologyId========"+technologyId);
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.COMMIT);
			String queryString = "from Skill s where s.technology.id = :technologyId";
			System.out.println("========queryString========"+queryString);
			Query query = session.createQuery(queryString);
			query.setCacheable(false).setLong("technologyId", technologyId);
			
			pSkills =(List<Skill>) query.list();
			System.out.println("PSkils LIST ::::::::::"+pSkills);
			for(Skill primarySkill : pSkills){
				primarySkills.add(primarySkill.getSkill());
				System.out.println("primarySkills====1111=="+primarySkills);
			}
			session.getTransaction().commit();
			return primarySkills;	
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}		
	}
 
 public List<RecruitmentRequest> getRRFSByEmployeeId(Long employeeId){ 
	 List<RecruitmentRequest> rrfs ;	
	 System.out.println("INSIDE RRF DAO EMP ID ::::::::"+employeeId);
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.COMMIT);
			String queryString = "from RecruitmentRequest rr where rr.employee.employeeId = :employeeId";
			
			Query query = session.createQuery(queryString);
			query.setCacheable(false).setLong("employeeId", employeeId);
			rrfs = (List<RecruitmentRequest>) query.list();
			System.out.println("SIZE OF RRFS : "+rrfs.size());
			session.getTransaction().commit();
			
			return rrfs;
			
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}	
	}
 
 public RecruitmentRequest updateRRFRequest(RecruitmentRequest rrfRequest){
		try{
		 Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		 session.beginTransaction();
		 session.setFlushMode(FlushMode.COMMIT);
		 session.saveOrUpdate(rrfRequest);
		 session.getTransaction().commit();
		 return rrfRequest;	
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
 
 public RecruitmentRequest getRRFByRecruitmentRequestCode(String rrfCode){
 	System.out.println("RRF CODE :::::DAO1111111:::"+rrfCode);
 	RecruitmentRequest recruitmentRequest;
 	try{
 		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
 		session.beginTransaction();
 		session.setFlushMode(FlushMode.AUTO);
 		String queryString = "from RecruitmentRequest rr where rr.recruitmentRequestCode =:rrfCode";
 		Query query = session.createQuery(queryString).setString("rrfCode", rrfCode);
 		query.setCacheable(false);
 		recruitmentRequest = (RecruitmentRequest) query.uniqueResult();
 		System.out.println("recruitmentRequest=====OBJ DAO===="+recruitmentRequest);
 		
 		
 		
 		session.getTransaction().commit();
 		return recruitmentRequest;
 	}catch (Exception e) {
 		e.printStackTrace();
 		return null;			
		}
 	
 }
/* public RecruitmentRequest getRRFByRecruitmentRequestCode(String recruitmentRequest1){ //EmployeeDAO
	 RecruitmentRequest recruitmentRequest ;		
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.COMMIT);
			String queryString = "from RecruitmentRequest rr where rr.recruitmentRequestCode = :recruitmentRequest1";
			
			Query query = session.createQuery(queryString);
			query.setCacheable(false).setString("recruitmentRequest1", recruitmentRequest1);			
			recruitmentRequest = (RecruitmentRequest) query.uniqueResult();
			session.getTransaction().commit();
			
			return recruitmentRequest;
			
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}	
	}*/
 public List<RecruitmentRequest> getAllRRFs() { 
		List<RecruitmentRequest> allRRFs = null;
		try {
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.COMMIT);
			Query query = session.createQuery("from RecruitmentRequest rr");
			query.setCacheable(false);
			allRRFs = (List<RecruitmentRequest>) query.list();
			session.getTransaction().commit();

		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return allRRFs;
	}

 public List<RecruitmentRequest> getAllRRFSByRecruiterId(Long employeeId){
	 List<RecruitmentRequest> rrfs ;	
	 System.out.println("INSIDE RRF DAO EMP ID ::::::::"+employeeId);
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.COMMIT);
			String queryString = "from RecruitmentRequest rr where rr.recruiter.employeeId = :employeeId";
			
			Query query = session.createQuery(queryString);
			query.setCacheable(false).setLong("employeeId", employeeId);
			rrfs = (List<RecruitmentRequest>) query.list();
			System.out.println("SIZE OF RRFS : "+rrfs.size());
			session.getTransaction().commit();
			
			return rrfs;
			
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}	
  }
 public List<RecruitmentRequest> getAllRRFSByInterviewerId(Long employeeId){
	 List<RecruitmentRequest> rrfs ;	
	 System.out.println("INSIDE RRF DAO EMP ID ::::::::"+employeeId);
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.COMMIT);
			String queryString = "from RecruitmentRequest rr where rr.interviewer1.employeeId = :employeeId or rr.interviewer2.employeeId = :employeeId";
			
			Query query = session.createQuery(queryString);
			query.setCacheable(false).setLong("employeeId", employeeId);
			rrfs = (List<RecruitmentRequest>) query.list();
			System.out.println("SIZE OF RRFS : "+rrfs.size());
			session.getTransaction().commit();
			
			return rrfs;
			
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}	
  }
 public static void main(String a[]){
	 System.out.println(new RRFDAO().getRRFByRecruitmentRequestCode("E50003-RRF-1"));
 }

}
